import { useDistInfoStore } from '@/stores/dict.js'
import { getGoodsDim,getStoreByProduct } from '@/api/BusinessScope/commodityControl'

const type = ref('商品维度')
const itemDetail = ref({})
const storeItemDetail = ref({})
// 经营品牌
const dictStore = useDistInfoStore()
const brandList = dictStore.getDistInfo('经营品牌').map(item=>({label:item.permissionName,value:item.id}))
// 门店分组
const storeGrouping = dictStore.getDistInfos('门店分组').map(item => ({ label: item.permissionName, value: item.id }))
export const useDimensionality= (props) => {
  return {
    brandList,
    type,
    itemDetail,
    storeItemDetail,
    storeGrouping
  }
}

export const useProduct = (props) => {
  const form = ref({
    page:{
      size:10,
      current:1
    },
    classify:1,
    mallInfoPut:null,
    businessBrandIdList:[],
    fuzzyValue:''
  })

  // 商品类型
  const productType = [ {
    label: '产品',
    value: 1
  },{
    label: '项目',
    value: 2
  },{
    label: '套餐',
    value: 3
  },
  // {
  //   label: '疗程',
  //   value: 7
  // }
]

  // 允许上架
  const shelvesAllowedList = [{
    label: '允许',
    value: 1
  },{
    label: '不允许',
    value: 2
  }]

  // 列表
  const tableData = ref([])
  const total = ref(0)
  const loading = ref(false)
  const search = async () => {
    try {
      loading.value = true
      const res = await getGoodsDim(form.value)
      tableData.value = res.records
      total.value = res.total
      if(!itemDetail.value.id){
        itemDetail.value = res.records[0]
      }
    }finally{
      loading.value = false
    }
  }
  const rest = () => {
    form.value = {
      page:{
        size:10,
        current:1
      },
      classify:1,
      mallInfoPut:null,
      businessBrandIdList:[],
      fuzzyValue:''
    }
    search()
  }
  return {
    form,
    productType,
    brandList,
    shelvesAllowedList,
    tableData,
    search,
    rest,
    itemDetail,
    total,
    loading
  }
}

export const useStore = (props) => {
  const form = ref({
    brandIds:[],
    keywords:'',
    storeGroupIds:[],
    page:{
      size:10,
      current:1
    }
  })
  const tableData = ref([])
  const total = ref(0)
  const loading = ref(false)
  const search = async () => {
    try{
      loading.value = true
      const res = await getStoreByProduct(form.value)
      tableData.value = res.records
      total.value = res.total
      if(!storeItemDetail.value.id){
        storeItemDetail.value = res.records[0]
      }
    }finally{
      loading.value = false
    }
  }
  const rest = () => {
    form.value = {
      storeId: '',
      storeName: '',
      storeGroupIds: [],
      page: {
        size: 10,
        current: 1
      }
    }
    search()
  }
  return {
    form,
    tableData,
    search,
    rest,
    total,
    loading,
    brandList,
    storeItemDetail,
    storeGrouping
  }
}


